var util = new NT.utilObj.util();
var loginUserName;
var orgCode;
var orgPath;
var orgName;
var orgId;
/*
 * 用户列表显示
 */
var queryGridUrl = "../loan/assignerfindByCondition.action";


/*
 *查询用户资料 
 */
var querDetialUrl = "../loan/findById.action";

var nowOperate;

var store_old={};
var store_new={};
function compareStore(){
	var returnValue=1;
	$.each(store_new,function(key,ele){
			
			var valueOld=store_old[key];
			if(ele!=valueOld){
				returnValue=0;
			}
		}
	);
	return returnValue;
}

function sortArr(arr){
	for(var i=0;i<arr.length;i++){
		for(var j=i+1;j<arr.length;j++){
			if(arr[i]>arr[j]){
				var temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
	return arr;
}

/**
 * 加载数据
 */
var loadData = function(){
	var headParam = [];
	//headParam.push("id");
	headParam.push("loanId");
	headParam.push("initTime");
	//业务种类
	headParam.push("loanType");
	//申贷金额
	headParam.push("loanAmount");
	//贷款期限
	headParam.push("loanTerm");
	//贷款人
	headParam.push("borrowerName");
	//处理进度
	headParam.push("processStat");
	//处理人
	headParam.push("curProcessHandler");
	//处理人批复意见
	headParam.push("curProcessFeedback");
	//进度更新日期
	headParam.push("curProcessUpdateTime");
	
	
	var url = queryGridUrl;
	
	var defaultBtns = {"viewBtn":"show","editBtn":"hidden","removeBtn":"hidden"};
	var operateBtns = [];
	var historyBtn = {'btnName':'historyBtn','text':'<button class="btn btn-sm btn-info" onclick="showHistory(this)" title="查看历史">' +
			' <i class="glyphicon glyphicon-inbox"></i>' +
			'</button>'};
	operateBtns.push(historyBtn);
	var querParam = getQueryGridParam();
	if(querParam!=null)
	{
		var gridObj = {};
		gridObj["url"] = url;
		gridObj["headParam"] = headParam;
		gridObj["queryParam"] = querParam;
		gridObj["defaultBtns"] = defaultBtns;
		gridObj["operateBtns"] = operateBtns;
		gridObj["pk"] = "loanId";
		//如果需要获取列表中其余字段值
		gridObj["columnValue"] = "processStat";
		gridObj["page"] = true;
		
		gridObj["xuhao"]=true;
		
		
		var nTGridBean = new NTGridBean();
		nTGridBean.init(gridObj);
		nTGridBean.loadGrid();
	}
};


/**
 * 获得pk
 */
var getPk = function(btn){
	var pk = $(btn).parent().parent().parent().attr("pk");
	return pk;
};
/**
 * 获得columnValue
 */
var getColumn = function(btn){
	var columnValue = $(btn).parent().parent().parent().attr("columnValue");
	return columnValue;
};

/**
 * 清空form
 */
var resetAddOrEditForm = function(){
	$("#addOrEditForm")[0].reset();
	$('span[id="errormsg"]').remove();
};


/**
 * 详情
 */
var viewData = function(param,viewSuccessFun){
	operateUtil.viewData(querDetialUrl,param,viewSuccessFun);
};


/**
 * 查看历史
 */
var showHistory = function(btn){
	var id = getPk(btn);
	var loanId = getPk(btn)
	var url = "../loanAppHistory/detailsLoanAppHistory.html?loanId="+loanId+"&loginUserName="+loginUserName;
	window.location.href = encodeURI(url);
};



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){
	window.checkForm.openFun().init({path:webPath,form:'addOrEditForm'}); 
	
	
	$("#queryGridBtn").click(function(){
		loadData();
	});
	
	//查询条件：当前用户需要从后台获取
	getLoginInfoAndLoadData();
	
});

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function getLoginInfoAndLoadData() {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : '../login/getLoginUserInfo.action',
		success : function(data) {
			var obj = eval('(' + data + ')');
			loginUserName = obj.data.userName;
			orgCode = obj.data.orgCode;
			orgPath = obj.data.orgPath;
			orgName = obj.data.orgName;
			orgId = obj.data.orgId;
			loadData();
			
		}
	});
};


/**
 * 详情
 */
var viewBtn = function(btn){
	var loanId = getPk(btn);
	var url = "../loanAppDetails/detailsLoanApp.html?loanId=" + loanId;
	//var url ="../UI/css/checkboxAndRedio/index.html"
	window.location.href = encodeURI(url);
};


/**
 * 获得查询列表参数
 */
var getQueryGridParam = function(){
	var QNum = $("#query_loanId").val();
	var QType =$("#query_bussniessType").val()=="0"?"":$("#query_bussniessType").val();
	var QStat =$("#query_processStat").val()=="0"?"":$("#query_processStat").val();
	var QName = $("#query_borrowName").val();
	var startTime=$("#statisticStartTime").val();
	//var endTime=getFormatDateStr($("#statisticEndTime").val(),1);
	var endTime=$("#statisticEndTime").val();
	/*if(startTime=="")
	{util.sysTips("开始时间必须选择","statisticStartTime"); return null;}
	else if(endTime=="")
	{util.sysTips("结束时间必须选择","statisticEndTime"); return null;}
	else */
	if(startTime!=""&&endTime=="")
	{
		util.sysTips("开始时间和结束时间必须同时输入","statisticEndTime"); return null;
	}
	else if(startTime==""&&endTime!="")
	{
		util.sysTips("开始时间和结束时间必须同时输入","statisticStartTime"); return null;
	}
	else if(startTime!=""&&endTime!=""&&endTime<=startTime)
	{util.sysTips("结束时间必须大于开始时间","statisticEndTime"); return null;}
	else{
		var param = {
				'loanId' : QNum,
				'loanType' : QType,
				'processStat' : QStat,
				'borrowerName' : QName,
				'queryStartTime' : startTime,
				'queryEndTime' : endTime
				//'operator' : loginUserName//,
				//'curProcessName':'提交申请'
			};
		return param;
	}
	
	
};

//$("#statisticStartTime").val(GetDateStr(-30));
//$("#statisticEndTime").val(GetDateStr(0));
$("#statisticStartTime").datetimepicker({
	format: "yyyy-mm-dd",
	autoclose:true,
	startView: 2,
	minView: 2,
	language:'cn'
});
$("#statisticEndTime").datetimepicker({
	format: "yyyy-mm-dd",
	autoclose:true,
	startView: 2,
	minView: 2,
	language:'cn'
});


/**
 *将json字符串转化为json对象
 */
function str2Json(jsonStr){
	var json = eval("(" + jsonStr + ")"); 
	return json;
};
